package com.hzdl.car.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Role;
import com.hzdl.car.bean.User;
import com.hzdl.car.mapper.RoleMapper;
import com.hzdl.car.mapper.UserMapper;
import com.hzdl.car.service.UserService;
import com.hzdl.car.vo.RoleVo;
import com.hzdl.car.vo.UserVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper mapper;
    @Resource
    private RoleMapper roleMapper;
    @Override
    public User login(UserVo userVo) {
        return mapper.login(userVo);
    }

    @Override
    public PageInfo<User> findUser(UserVo userVo) {
        PageHelper.startPage(userVo.getPage(), userVo.getLimit());
        List<User> list = mapper.findUser(userVo);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int addUser(UserVo userVo) {
        return mapper.addUser(userVo);
    }

    @Override
    public int updateUser(UserVo userVo) {
        return mapper.updateUser(userVo);
    }

    @Override
    public int chkRoleUser(UserVo userVo) {
        return mapper.chkRoleUser(userVo);
    }
    @Transactional
    @Override
    public int delUser(UserVo userVo) {
        int row=0;
        row+=mapper.delUser(userVo);
        row+=mapper.delRoleUser(userVo);
        return row;
    }

    @Override
    public int resetPwd(UserVo userVo) {
        userVo.setPwd("123456");
        return mapper.resetPwd(userVo);
    }
    @Transactional
    @Override
    public int userDelChk(Integer[] userIds) {
        int row=0;
        for(Integer i:userIds){
            UserVo userVo=new UserVo();
            userVo.setUserId(i);
            row+=mapper.delUser(userVo);
        }
        return row;
    }

    /**
     * 分配角色显示
     * @param userVo
     * @return
     */
    @Override
    public List<Map<String, Object>> findRoleByUser(UserVo userVo) {
        //查询所有的角色
        RoleVo roleVo=new RoleVo();
        roleVo.setAvailable(1);
        List<Role> roles=roleMapper.findRole(roleVo);
        //当前用户角色
        List<Role> roleUser=roleMapper.findRoleByUser(userVo.getUserId());

        List<Map<String,Object>> roleUsers = new ArrayList<>();
        for(Role r1:roles){
            Boolean  LAY_CHECKED = false;
            for (Role r2:roleUser){
                if(r1.getRoleId()==r2.getRoleId()){
                    LAY_CHECKED=true;
                    break;
                }
            }
            Map<String,Object> map = new HashMap<>();
            map.put("roleId",r1.getRoleId());
            map.put("roleName",r1.getRoleName());
            map.put("roleDesc",r1.getRoleDesc());
            map.put("LAY_CHECKED",LAY_CHECKED);
            roleUsers.add(map);

        }
        return roleUsers;
    }
    @Transactional
    @Override
    public void saveRoleUser(UserVo userVo) {
        mapper.deleteRoleByUser(userVo.getUserId());
        for(Integer id:userVo.getIds()){
            mapper.addRoleUser(userVo.getUserId(),id);
        }
    }
}
